package org.boot.ssm.wxshop.web.controller;

import com.github.pagehelper.PageInfo;
import org.boot.ssm.wxshop.domain.News;
import org.boot.ssm.wxshop.service.NewsService;
import org.boot.ssm.wxshop.web.common.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @program: wxshop
 * @description: 新闻控制器
 * @author: Parasomnia
 * @create: 2018-03-16 13:47
 **/
@Controller
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    /**
     * 到新闻列表页面
     * @param model
     * @return
     */
    @RequestMapping("/front/allNews")
    public String allNews(Model model){
        News news = new News();
        news.setIsDelete(1);
        model.addAttribute("newsList",newsService.queryParamList(news));
        return "frontstage/news/news_list";
    }

    /**
     * 跳转到新闻详情
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/front/detail")
    public String newsDetail(Integer id,Model model){
        model.addAttribute("news",newsService.queryById(id));
        return "frontstage/news/news_info";
    }

    /**
     * 前台列表新闻加载 ajax
     * @return
     */
    @RequestMapping("/front/newsList")
    @ResponseBody
    public JsonResult newsList(){
        News news = new News();
        news.setIsDelete(1);
        return new JsonResult(0,"加载成功",newsService.queryParamList(news));
    }



    //=================================================后台=================================================================================


    @RequestMapping("/goNewsmgr")
    public String newsMgr(){
        return "backstage/news/mgr";
    }

    /**
     * 去添加或修改页面
     * @param id 获取的新闻id
     * @param model request
     * @return
     */
    @RequestMapping("/goAddOrEdit")
    public String goAddOrEdit(Integer id,Model model){
        if(id!=null){//修改
            News news = newsService.queryById(id);
            model.addAttribute("news",news);
        }
        return "backstage/news/addoredit";
    }

    @RequestMapping("/newsList")
    @ResponseBody
    public JsonResult newsList(Integer page,Integer limit){
        News news = new News();
        news.setIsDelete(1);
        PageInfo<News> pageInfo = newsService.queryPageListByParam(page,limit,news);
        return new JsonResult(0,"加载成功",pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 单个删除
     * @param news
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(News news){
        newsService.updateDelete(news);
        return new JsonResult(0,"删除成功");
    }

    /**
     * 批量删除
     * @return
     */
    @RequestMapping("/deleteMany")
    @ResponseBody
    public JsonResult delete(@RequestBody List<Integer> ids){
        newsService.updateDeleteMany(ids);
        return new JsonResult(0,"批量删除成功");
    }


    @RequestMapping("/addOrEdit")
    @ResponseBody
    public JsonResult addOrEdit(News news){
        newsService.addOrEdit(news);
        return new JsonResult(200,"操作成功");
    }

}
